#include <51func.h>
#include <mysql/mysql.h>
int main(int argc, char *argv[])
{
    MYSQL * mysql = mysql_init(NULL); // 分配内存 初始化
    MYSQL * ret = mysql_real_connect(
                                     mysql,
                                     "localhost",
                                     "root",
                                     "123",
                                     "51test",
                                     0,NULL,0
                                    );
    if(ret == NULL){
        fprintf(stderr,"mysql_real_connect:%s\n", mysql_error(mysql));
        return -1;
    }
    char sql[1024] = "select * from owners;";
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    printf("row = %lu, col = %u\n",mysql_num_rows(res), mysql_num_fields(res));
    while(1){
        MYSQL_ROW row = mysql_fetch_row(res);//取出一行
        if(row == NULL){
            break;
        }
        for(unsigned j = 0; j < mysql_num_fields(res); ++j){
            printf("%s\t", row[j]);
        }
        printf("\n");
    }
    mysql_free_result(res);
    strcpy(sql,"desc owners;");
    qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    res = mysql_store_result(mysql);
    printf("row = %lu, col = %u\n",mysql_num_rows(res), mysql_num_fields(res));
    while(1){
        MYSQL_ROW row = mysql_fetch_row(res);//取出一行
        if(row == NULL){
            break;
        }
        for(unsigned j = 0; j < mysql_num_fields(res); ++j){
            printf("%s\t", row[j]);
        }
        printf("\n");
    }
    mysql_free_result(res);
    mysql_close(mysql); // 回收资源
    return 0;
}

